package com.hrm.company.service;

import com.hrm.common.service.BaseService;
import com.hrm.common.util.IdWorker;
import com.hrm.company.dao.DepartmentDao;
import com.hrm.domain.company.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentService extends BaseService<Department> {
    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 保存部门
     */
    public void addDepartment(Department department) {
        String id = idWorker.nextId() + "";
        department.setId(id);
        departmentDao.save(department);
    }

    /**
     * 更新部门
     */
    public void updateDepartment(Department department) {
        Department temp = departmentDao.findById(department.getId()).get();
        temp.setName(department.getName());
        temp.setCode(department.getCode());
        temp.setIntroduce(department.getIntroduce());
        temp.setManager(department.getManager());
        temp.setManagerId(department.getManagerId());
        departmentDao.save(temp);
    }

    /**
     * id查询部门
     */
    public Department findDepartmentById(String id) {
        return departmentDao.findById(id).get();
    }

    /**
     * 查询全部部门列表
     */
    public List<Department> findDepartments(String companyId) {
        //根据企业id属性去查询所有的部门字段
        return departmentDao.findAll(setSpec(companyId));
    }

    /**
     * id删除部门
     */
    public void deleteDepartment(String id) {
        departmentDao.deleteById(id);
    }

    /**
     * 根据部门code和企业id查找
     *
     * @param code
     * @param companyId
     * @return
     */
    public Department findByCodeAndCompanyId(String code, String companyId) {
        return departmentDao.findByCodeAndCompanyId(code, companyId);
    }
}
